package com.globallogic.jaxrs;

import java.io.StringWriter;
import java.net.URI;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import com.globallogic.model.service.UserService;
import com.globallogic.model.transferobject.UserTo;

@Path("users")
@Stateless
public class UserWebService {

	@GET
	@Produces("text/html")
	public String sayHello() {
		return "<html lang=\"en\"><body><h1>Hello, World!!</h1></body></html>";
	}

	@GET
	@Produces("application/json")
	public UserList getUsers() {
//		StringWriter stringWriter = new StringWriter();
		List<UserTo> userToList = userService.getUsers();
		UserList userList = new UserList();

		if (null == userToList) {
			return null;
		} else {

			for (UserTo userTo : userToList) {
				userList.add(userTo);
			}
			
			return userList;
		}

		// if (null == userToList) {
		// return Response.serverError().build();
		// } else {
		//
		// for (UserTo userTo : userToList) {
		// userList.add(userTo);
		// }
		//
		// RsMarshaller.marshal(userList, stringWriter);
		// return Response.ok(stringWriter.toString()).build();
		// }
	}

	@GET
	@Path("{name}")
	@Produces({ "application/json" })
	public Response getUser(@PathParam("name") String name) {
		StringWriter userToXml = new StringWriter();
		UserTo userTo = userService.getUser(name);

		if (null == userTo) {
			return Response.status(Response.Status.NOT_FOUND)
					.entity(String.format("User with name %s not found", name))
					.build();
		} else {
			return Response.ok(userTo).build();
		}

		// if (null == userTo) {
		// return Response.status(Response.Status.NOT_FOUND)
		// .entity(String.format("User with name %s not found", name))
		// .build();
		// } else {
		// RsMarshaller.marshal(userTo, userToXml);
		// return Response.ok(userToXml.toString()).build();
		// }
	}

	@POST
	@Consumes({ "application/xml", "application/json" })
	public Response createUser(UserTo userTo) {
		int userId = userService.registerUser(userTo.getEntity());
		userTo.setId(userId);
//		return Response.created(URI.create("/" + userId)).build();
//		return Response.ok(userTo).build();
		return Response.status(201).entity(userTo).build();
	}

	@PUT
	@Consumes({ "application/xml", "application/json" })
	public Response updateOrCreateUser(UserTo userTo) {
		System.out.println("### updateOrCreateUser");
		int userId;

		if (null == userService.findUser(userTo.getName())) {
			System.out.println("### updateOrCreateUser createUser");

			userId = userService.registerUser(userTo.getEntity());
			return Response.created(URI.create("/" + userId)).build();
		} else {
			System.out.println("### updateOrCreateUser updateUser");
			userId = userService.updateUser(userTo);
			return Response.created(URI.create("/" + userId)).build();
		}
	}

	@DELETE
	@Path("{name}")
	@Consumes({ "application/xml", "application/json" })
	public Response deleteUser(@PathParam("name") String name) {
		boolean result = userService.deleteUser(name);
		if (result) {
			return Response.ok().build();
		} else {

			return Response.status(Response.Status.NOT_FOUND)
					.entity(String.format("User with name %s not found", name))
					.build();
		}
	}

	@EJB
	private UserService userService;

}
